package cn.my.springHDFS.Interceptor;

import cn.my.springHDFS.domain.SpiderStatus;
import cn.my.springHDFS.service.ManagementServiceImpl;
import cn.my.springHDFS.util.WebHDFSProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;

import java.io.File;
import java.util.Map;

/**
 * Created by Leo on 2017/6/24.
 * 定时任务
 */
@Component
public class Sceduler {

    @Autowired
    private ManagementServiceImpl managementServiceImpl;

    @Value("${master}")
    private String ip_Address;

    private String Path = "C:\\Users\\s\\Desktop\\WebHDFS_UploadHistory";


//    @Scheduled(initialDelay = 60 * 1000, fixedDelay = 60 * 1000)
    @Scheduled(fixedRate = 60 * 1000)
    public void spider(){
        //初始化Spider对象
        Spider spider = Spider.create(new WebHDFSProcessor());

        //返回爬虫状态以及Spider对象,获取response headers
        Spider Site_Status = managementServiceImpl.getWebHDFS_Status(spider, ip_Address);
        System.out.println(Site_Status);
        Map<String, String> headers = Site_Status.getSite().getHeaders();
        SpiderStatus.setIsActive(headers.get("IsActive"));
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void deleteFile(){
//        doDeleteEmptyDir("new_dir1");
        String newDir2 = "C:\\Users\\s\\Desktop\\WebHDFS_UploadHistory";
        boolean success = deleteDir(new File(newDir2));
        if (success) {
            System.out.println("Successfully deleted populated directory: " + newDir2);
            File f = new File("C:\\Users\\s\\Desktop\\WebHDFS_UploadHistory\\");
            f.mkdirs();
        } else {
            System.out.println("Failed to delete populated directory: " + newDir2);
        }
    }

    /**
     * 删除空目录
     * @param dir 将要删除的目录路径
     */
    private static void doDeleteEmptyDir(String dir) {
        boolean success = (new File(dir)).delete();
        if (success) {
            System.out.println("Successfully deleted empty directory: " + dir);
        } else {
            System.out.println("Failed to delete empty directory: " + dir);
        }
    }

    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * @param dir 将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful.
     *                 If a deletion fails, the method stops attempting to
     *                 delete and returns "false".
     */
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            assert children != null;
            for (String aChildren : children) {
                boolean success = deleteDir(new File(dir, aChildren));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }
}
